﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DormMS.IService;
using DormMS.Model;
using DormMS.Model.SearchModels;
using DormMS.Model.ViewModels;
using Infrastructure.Dao;
using SqlSugar;

namespace DormMS.Service
{
    public class BuildingService : IBuildingService
    {
        private SqlSugarClient db;
        /// <summary>
        /// 新增大楼信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int BuildingAdd(Building model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Insert<Building>(model).ObjToInt();
            }
        }
        /// <summary>
        /// 删除大楼信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int BuildingDel(Building model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Building>(model).ObjToInt();
            }
        }
        /// <summary>
        /// 编辑大楼信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int BuildingEdit(Building model)
        {
            using (db = DbConfig.GetInstance())
            {
                return db.Update<Building>(model).ObjToInt();
            }
        }

        /// <summary>
        /// 通过Id单个大楼信息
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        public Building GetBuildingInfoById(int Id)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Building>().SingleOrDefault(x => x.Id == Id);
                return model;
            }
        }

        /// <summary>
        /// 通过NO单个大楼信息
        /// </summary>
        /// <param name="BuildingNO"></param>
        /// <returns></returns>
        public Building GetBuildingInfoByNO(string NO)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Building>().SingleOrDefault(x => x.BuildingNO == NO);
                return model;
            }
        }

        /// <summary>
        /// 按检索条件获取大楼信息列表
        /// </summary>
        /// <param name="where">条件返回列表</param>
        /// <returns></returns>
        public List<Building> GetBuildingInfoList(string where)
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Building>().Where(where).OrderBy(t => t.BuildingNO).ToList();
                return model;
            }
        }

        /// <summary>
        /// 按检索条件获取大楼信息列表
        /// </summary>
        /// <param name="search"></param>
        /// <param name="total"></param>
        /// <returns></returns>
        public List<Building> GetBuildingList(BuildingSearchModel search, ref int total)
        {
            
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";

                search.OrderByFields = search.OrderByFields;
                var data = db.Queryable<Building>();
                if (string.IsNullOrEmpty(search.Keywords) == false )
                {
                    data = data.Where<Building>(x => x.BuildingNO == search.Keywords);
                }

               var list = data.Select<Building>((x) => new Building{Id = x.Id, BuildingNO = x.BuildingNO,Address = x.Address}).OrderBy(search.OrderByFields).ToPageList(search.Skip / search.Take + 1, search.Take, ref total);

                return list;
            }
        }

        /// <summary>
        /// 获得大楼信息所有列表
        /// </summary>
        /// <param></param>
        /// <returns></returns>
        public List<Building> GetBuildingAllList()
        {
            using (db = DbConfig.GetInstance())
            {
                var model = db.Queryable<Building>().OrderBy(t => t.BuildingNO).ToList();
                return model;
            }
        }

        public List<V_UserInfo> GetV_UserInfo(string name)
        {
            using (db = DbConfig.GetInstance())
            {
                db.CurrentFilterKey = "";

                return db.Queryable<V_UserInfo>().Where(v=>v.StudentName == name).ToList();
            }
        }
    }
}
